package webservices;

import authentication.IMembershipProvider;
import managers.WrongBidException;
import services.ProductsService;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public class ProductManagerWebService {

    private ProductsService productsService;
    private IMembershipProvider membershipProvider;

    public ProductsService getProductsService() {
        return productsService;
    }

    public void setProductsService(ProductsService productsService) {
        this.productsService = productsService;
    }

    public IMembershipProvider getMembershipProvider() {
        return membershipProvider;
    }

    public void setMembershipProvider(IMembershipProvider membershipProvider) {
        this.membershipProvider = membershipProvider;
    }

    @WebMethod(operationName="setBid")
    public boolean setBid(@WebParam(name="productId") int productId,
                          @WebParam(name="price") float price) {

        if(!membershipProvider.isAuthenticated()){
            return false;
        }

        try {
            productsService.placeBid(productId, membershipProvider.userName(), price);
        } catch (WrongBidException e) {
            return false;
        }

        return true;
    }
}
